package com.movie.controller.SysMovieController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.movie.beans.*;
import com.movie.beans.base.AjaxResult;
import com.movie.service.MovieService;
import com.movie.service.RateService;
import com.movie.service.SiteRateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.movie.utils.RateUtil.getAvgRateAndPoint;

@Controller
@RequestMapping("/system/movie")
public class SysMovieController {
    @Autowired
    private MovieService movieService;
    @Autowired
    private RateService rateService;
    @Autowired
    private SiteRateService siteRateService;
    private static String prefix = "/system/movie";

    @GetMapping()
    public String index(){
        return prefix+"/index";
    }

    @GetMapping("/insert")
    @ResponseBody
    public int insert(){
        List<Movies> list = movieService.MoviesList();
        int i = 1;
        System.out.println("共"+list.size()+"条数据----------------------");
        for (Movies m:list) {
            Double avgRate = rateService.selectRatingAvgByMovieId(m.getMovieId());
            Double rate = getAvgRateAndPoint(avgRate)[0];
            Double realRate = getAvgRateAndPoint(avgRate)[1];
            Double point = getAvgRateAndPoint(avgRate)[2];
            SiteRate siteRate = new SiteRate(m.getMovieId(),rate,point,realRate);
            int j = siteRateService.insert(siteRate);
            if(j>0){
                System.out.println("已插入"+i+"条数据");
                i++;
            }
        }
        System.out.println("结束----------------------");
        return i;
    }

    @PostMapping("/list")
    @ResponseBody
    public Map list(Movies movies, Integer limit, Integer page){
        Map map = new HashMap();
        PageHelper.startPage(page,limit);
        List<Movies> list = movieService.MoviesList();
        map.put("msg","");
        map.put("code",0);
        map.put("data",list);
        map.put("count",new PageInfo(list).getTotal());
        return map;
    }

    @PostMapping("/search")
    @ResponseBody
    public Map search(String searchVal,int limit,int page){
        Map map = new HashMap();
        PageHelper.startPage(page,limit);
        List<Movies> movies= movieService.getMoviesBySearchVal(searchVal);
        map.put("msg","");
        map.put("code",0);
        map.put("data",movies);
        map.put("count",new PageInfo(movies).getTotal());
        return map;
    }

    @GetMapping("/add")
    public String add(){
        return prefix+"/add";
    }


    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addMovie(String name, String genres){
        Movies movies=new Movies();
        movies.setTitle(name);
        movies.setGenres(genres);
        int t=movieService.insertMovies(movies);
        if (t>0){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }


}
